package com.naiterui.ehp.bs.weixin.controller;

import cn.hutool.core.util.StrUtil;
import com.naiterui.common.redis.RedisUtil;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.weixin.config.SubscribeConfig;
import com.naiterui.ehp.bs.weixin.constant.WeixinMaConstant;
import com.naiterui.ehp.bs.weixin.subscribe.SubscribeProperties;
import com.naiterui.ehp.bs.weixin.vo.SubscribeTemplateListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 微信服务首页
 *
 * @author yangfuya
 * @version 1.0
 * @date 2021/5/27 11:07
 **/
@Slf4j
@Api(tags = SwaggerTag.WEIXIN + SwaggerTag.BASIC_INFO)
@RestController
@RequestMapping("/index")
@RequiredArgsConstructor
public class IndexController {

    private final SubscribeConfig subscribeConfig;

    @GetMapping("/subscribe/templates")
    @ApiOperation("获取订阅消息模板列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型：1.问诊提醒(登陆后调用) 2.图文/视频问诊 3.处方支付 4.用药提醒", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "long", paramType = "query")
    })
    public List<SubscribeTemplateListVO> getSubscribeTemplateList(Integer type, Long patientId) throws BusinessException {
        if (type == null) {
            log.info("参数校验失败");
            throw new BusinessException(BaseExceptionCodes.PARAM_ERROR);
        }
        List<SubscribeProperties> list = subscribeConfig.getTemplate();
        return list.stream().map(s -> {
            SubscribeTemplateListVO templateListVO = null;
            // 问诊提醒
            if (SubscribeTemplateListVO.TYPE_INQUIRY_REMINDER == type) {
                if (SubscribeTemplateListVO.LABEL_INQUIRY_REMINDER.equals(s.getLabel())) {
                    if(null != patientId){
                        String hget = RedisUtil.hashOps().hget(WeixinMaConstant.WECHAR_MA_SUBSCRIBE, SubscribeTemplateListVO.LABEL_INQUIRY_REMINDER + "-" + patientId);
                        if(StrUtil.isNotBlank(hget) && "accept".equals(hget)){
                            return null;
                        }
                    }
                    templateListVO = SubscribeTemplateListVO.builder().templateId(s.getTemplateId()).title(s.getTitle()).build();
                }
            }
            // 问诊
            else if (SubscribeTemplateListVO.TYPE_INQUIRY == type) {
                if (SubscribeTemplateListVO.LABEL_SERVICE_EVALUATION.equals(s.getLabel())
                    || SubscribeTemplateListVO.LABEL_REFUND_NOTICE.equals(s.getLabel())
                    || SubscribeTemplateListVO.LABEL_PRESCRIPTION_PAY.equals(s.getLabel())) {
                    templateListVO = SubscribeTemplateListVO.builder().templateId(s.getTemplateId()).title(s.getTitle()).build();
                }
            }
            // 购药
            else if (SubscribeTemplateListVO.TYPE_BUY_MEDICINE == type) {
                if (SubscribeTemplateListVO.LABEL_SHIP_NOTICE.equals(s.getLabel())
                        || SubscribeTemplateListVO.LABEL_RECEIPT_REMINDER.equals(s.getLabel())
                        || SubscribeTemplateListVO.LABEL_REFUND_NOTICE.equals(s.getLabel())) {
                    templateListVO = SubscribeTemplateListVO.builder().templateId(s.getTemplateId()).title(s.getTitle()).build();
                }
            }
            // 购药
            else if (SubscribeTemplateListVO.TYPE_ADVISORY_REMINDER == type) {
                if (SubscribeTemplateListVO.LABEL_ADVISORY_REMINDER.equals(s.getLabel())) {
                    templateListVO = SubscribeTemplateListVO.builder().templateId(s.getTemplateId()).title(s.getTitle()).build();
                }
            }
            return templateListVO;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }
}
